package com.kitty.game.task.service.taskHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.CardAttrib;
import com.kitty.game.item.config.ChangeCard;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.task.TaskDataPool;
import com.kitty.game.task.message.vo.TaskInfo;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.utils.FieldUtils;
import com.kitty.game.utils.TimeUtil;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;


/**
 * 变身卡任务
 */
@Component
public class ChangeCardTaskHandler extends TimeTaskHandler {
    private static final List<String> polarNames = Arrays.asList("无", "金", "木", "水", "火", "土");
    private static final String EXPIRE_TIPS = "{0}已经过期，系统自动清除。";

    @Override
    public TaskType getTaskType() {
        return TaskType.CHANGE_CARD;
    }

    @Override
    public TaskInfo getTaskInfo(Role role, TaskSet taskSet) {
        StringBuilder attribTips = new StringBuilder();
        ChangeCard changeCard = role.getChangeCard();
        changeCard.getAttrib().forEach(cardAttrib -> attribTips.append(getCardInfo(cardAttrib)));
        StringBuilder battleArr = new StringBuilder();
        changeCard.getBattleAttrib().forEach(cardAttrib -> battleArr.append(getCardInfo(cardAttrib)));
        TaskInfo taskInfo = super.getTaskInfo(role, taskSet);
        taskInfo.setTaskZhiyin(MessageFormat.format(taskSet.getTaskZhiyin(), changeCard.getName(), getExpireString(changeCard.getExpireTime())));
        taskInfo.setTaskInfo(MessageFormat.format(taskSet.getTaskJieshao(), changeCard.getName(), getPolarString(changeCard.getPolar()), getExpireString(changeCard.getExpireTime())));
        taskInfo.setTaskJiangli(MessageFormat.format(taskSet.getTaskGift(), attribTips.substring(0, attribTips.length() - 1),battleArr.length()>0? battleArr.substring(0, battleArr.length() - 1):""));
        return taskInfo;
    }

    private String getCardInfo(CardAttrib cardAttrib){
        StringBuilder attribTips =new StringBuilder();
        attribTips.append("#B");
        attribTips.append(cardAttrib.getChsName());
        if(cardAttrib.getValue()>0){
            attribTips.append(" +");
        }else {
            attribTips.append(" ");
        }
        attribTips.append(cardAttrib.getValue());
        attribTips.append("%#n、");
        return attribTips.toString();
    }

    public boolean acceptTask(Role role) {
        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.CHANGE_CARD_TASKID, role);
        return acceptTask(role, taskSet);
    }

    public void delTask(Role role) {
        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.CHANGE_CARD_TASKID, role);
        /**任务已经接取时，删除原任务再添加一个*/
        if (taskService.isTaskAccepted(role, taskSet)) {
            giveUpTask(role, taskSet);
        }
    }

    @Override
    public void doBeforeAccept(Role role, TaskSet taskSet) {
        super.pushEndTask(role, taskService.getTaskSet(TaskDataPool.CHANGE_CARD_TASKID, role));
    }

    /**
     * 能否放弃任务
     */
    @Override
    public boolean canGiveUpTask(Role role, TaskSet taskSet) {
        return true;
    }

    /**
     * 处理任务倒计时
     */
    @Override
    public void handleHeartBeat(Role role, long prevHeartTime) {
        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.CHANGE_CARD_TASKID, role);
        /**如果有任务就继续下面*/
        if (taskService.isTaskAccepted(role, taskSet)) {
            ChangeCard changeCard = role.getChangeCard();
            if (changeCard != null) {
                long expireTime = changeCard.getExpireTime();
                long currTime = System.currentTimeMillis();
                /**处理的时间多-1秒吧 如果没有过期 就刷新*/
                if (expireTime - TimeUtil.ONE_SECOND > currTime) {
                    pushTask(role, taskSet);
                } else {/**过期了就删除任务 刷新角色外观*/
                    giveUpTask(role, taskSet);
                    MessagePusher.pushMessage(role, new RespMsg(MessageFormat.format(EXPIRE_TIPS, changeCard.getName())));
                }
            }
        }
    }

    /**
     * 进行任务放弃前的操作
     */
    @Override
    public void doBeforeGiveUpTask(Role role, TaskSet taskSet) {
        /**放弃任务 删除变身卡 刷新外观 更新增益属性*/
        role.setChangeCard(null);

        FieldUtils.sendChangeIcon(role);
        /**更新外观*/
        SpringUtils.getBroadcastService().sendUpdateAppear(role);

        /**发送增益属性*/
        SpringUtils.getRoleService().sendExtraAttribute(role);


    }

    /**
     * 返回过期时间 Example XX小时XX分钟
     */
    private String getExpireString(long expireTime) {
        long lastTime = expireTime - System.currentTimeMillis();
        int hour = (int) (lastTime / TimeUtil.ONE_HOUR);
        int minute = (int) ((lastTime - hour * TimeUtil.ONE_HOUR) / TimeUtil.ONE_MINUTE);
        if (hour >= 1) {
            if (minute >= 30) {
                hour += 1;
            }
            return hour + "小时";
        } else {
            if (minute == 0) {
                minute += 1;
            }
            return minute + "分钟";
        }
    }

    /**
     * 取相性
     */
    public String getPolarString(int polar) {
        return polarNames.get(polar);
    }
}
